package MtG.client.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import MtG.client.panels.cardPanelComponents.gameGards.Card;
import MtG.publicClass.Player;

/**
 * GamePanel.java is the class that designs the games "board" area 
 * where all the cards in play will be situated.
 * @author Marek Sirp
 * @author Jaan Sepp
 */
public class GamePanel extends JPanel{
	
	/**
	 * Determines if a de-serialized file is compatible with this class.
	 */
	private static final long serialVersionUID = 720253733778723154L;
	/**
	 * Upper panel for player2 cards
	 */
	private JPanel upperPanel = new JPanel();
	/**
	 *Lower panel for player1 cards
	 */
	private JPanel lowerPanel = new JPanel();
	/**
	 * New layout for upperPanel and lowePanel
	 */
    /**
     * Preferred height of the different card panels.
     */
    private final int CARDPANEL = 175;
	/**
	 * Create the message panel for all the server messages.
	 */
	private MessagePanel serverArea = new MessagePanel("Textarea for server messages");
    /**
     * Creating the scroll pane for the server message area.
     */
    private JScrollPane scroll= new JScrollPane(serverArea);
		
	/**
	 * Constructor.
	 */
	public GamePanel(){
		setName("GamePanel");
		setBackground(Color.GRAY);
		setLayout(new BorderLayout());
		upperPanel.setPreferredSize(new Dimension(0, CARDPANEL));
		upperPanel.setName("upperPanel");
		upperPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
		add(upperPanel, BorderLayout.NORTH);
		lowerPanel.setPreferredSize(new Dimension(0, CARDPANEL));
		lowerPanel.setName("lowerPanel");
		lowerPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
		add(lowerPanel, BorderLayout.SOUTH);
		add(scroll, BorderLayout.CENTER);			
	}
	
	/**
	 * @return reference to the server message area.
	 */
	public MessagePanel getServerArea(){
		return serverArea;
	}

	
	/**
	 * @param player reference to the current player.
	 * @param playerRoll string that will be used to determine onto which part of the panel the card will be placed.
	 */
	public void placeCardstoPlay(Player player, String playerRoll){
		JPanel panel;
		if(playerRoll.equals("")){
			upperPanel.removeAll();
			lowerPanel.removeAll();
			lowerPanel.revalidate();
			lowerPanel.repaint();
			upperPanel.revalidate();
			upperPanel.repaint();
		} else {
		if(playerRoll.equals("player2")){
			panel = upperPanel;
		} else {
			panel = lowerPanel;
		}
		panel.removeAll();		
		for(MtG.publicClass.Card c : player.getPlayersCardsInPlay()){
			Card placeableCard = new Card(c);
			panel.add(placeableCard);
		}
		panel.revalidate();
		panel.repaint();
		}


		
	}

}
